<template>
  <div>
    <!-- 头部面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/wlecome' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item :to="{path:'/params'}">参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="roles_card" style="margin-top: 10px">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon="">
      </el-alert>
      <!-- 选择分类的级联框 -->
      <el-form ref="form" label-width="100px" style="margin-top:15px">
        <el-form-item label="选择商品分类:">
          <el-cascader
            expand-trigger="hover"
            :props="props_config"
            :options="fei_lei_lists"
            v-model="fei_lei__id_keys"
            @change="value_change_onfun">
          </el-cascader>
        </el-form-item>
      </el-form>
      <!-- Tab选项卡 -->
      <el-tabs @tab-click="tabs_change_onfun" v-model="activeName">
        <el-tab-pane label="动态参数" name="only">
          <!-- 添加参数按钮 -->
          <el-button @click="show_add_params_onfun" type="primary">添加参数</el-button>
          <!-- 参数列表 -->
          <el-table :data="params_lists" style="width: 100%" stripe border fit>
              <el-table-column type="expand">
                <template slot-scope="scope">
                  <el-tag @close="del_params_tag_onfun(scope.row, index)" closable :key="index" v-for="(itme, index) in scope.row.attr_vals" type="success">{{itme}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column type="index" label="#">
              </el-table-column>
              <el-table-column prop="attr_name" label="参数名称">
              </el-table-column>
              <el-table-column>
                  <template>
                      <el-button type="primary" icon="el-icon-edit">编辑</el-button>
                      <el-button type="danger" icon="el-icon-delete">删除</el-button>
                  </template>
              </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="many">
          <!-- 添加属性按钮 -->
          <el-button @click="show_add_params_onfun" type="primary">添加属性</el-button>
          <!-- 参数列表 -->
          <el-table :data="params_lists" style="width: 100%" stripe border fit>
              <el-table-column type="expand">
                <template slot-scope="scope">
                  <el-tag @close="del_params_tag_onfun(scope.row, index)" closable :key="index" v-for="(itme, index) in scope.row.attr_vals" type="success">{{itme}}</el-tag>
                </template>
              </el-table-column>
              <el-table-column type="index" label="#">
              </el-table-column>
              <el-table-column prop="attr_name" label="参数名称">
              </el-table-column>
              <el-table-column>
                  <template>
                      <el-button type="primary" icon="el-icon-edit">编辑</el-button>
                      <el-button type="danger" icon="el-icon-delete">删除</el-button>
                  </template>
              </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
    export default {
      data () {
        return {
          fei_lei_lists:[],
          fei_lei__id_keys:[],
          activeName:'only',
          // 参数数据
          params_lists:[],
          props_config:{
            label:'cat_name',
            value:'cat_id',
            children:'children'
          }
        }
      },
      methods: {
        // 获取商品分类数据的函数
        async get_goods_feilei_lists() {
          const {data:res_data} = await this.$axios.get('categories');
          if(res_data.meta.status != 200) return this.$Message.error('获取分类失败!');
          this.fei_lei_lists = res_data.data;
          return this.$Message.success('获取分类成功');
        },
        // 获取参数的请求函数
        async get_params_lists() {
          const {data: res_data} = await this.$axios.get(`categories/${this.fei_lei__id_keys[this.fei_lei__id_keys.length - 1]}/attributes`, {
            params:{
              sel:this.activeName
            }
          });
          if(res_data.meta.status != 200) return this.$Message.error('错误啦');
          this.params_lists = res_data.data;
          this.params_lists.forEach(itme => {
              itme.attr_vals = itme.attr_vals.split(" ");
          });
          return this.$Message.success('成功嘞!');
        },
        // 选择分类即级联框 发生改变触发
        value_change_onfun() {
          if(this.fei_lei__id_keys.length === 3) {
            this.get_params_lists();
          }
        },
        show_add_params_onfun() {

        },
        // 修改编辑配置参数亲求函数
        async set_params_infor_tag(row) {
          var fen_lei_id = this.fei_lei__id_keys[this.fei_lei__id_keys.length - 1];
          const {data: res_data} = await this.$axios.put(`categories/${fen_lei_id}/attributes/${row.attr_id}`, {
            attr_name:row.attr_name,
            attr_sel:row.attr_sel,
            attr_vals:row.attr_vals.join(" ")
          });
          console.log(res_data);
        },
        // dianji删除params tag标签时触发
        del_params_tag_onfun(row, index) {
          row.attr_vals.splice(index, 1);
          // 删除tag标签
          this.set_params_infor_tag(row);
        },
        tabs_change_onfun() {
          this.get_params_lists();
        }
      },
      mounted () {
        this.get_goods_feilei_lists();
      }
    }
</script>

<style>

</style>